﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeStoryManager : IStoryManager   
    {
        private List<Story> storyList;

        public FakeStoryManager(List<Story> storys)
        {
            storyList = storys;
        }

        /// <summary>
        /// Create a story (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Story NewStory()
        {
            var story = new Story();
            
            story.CreateTime = DateTime.Now;            

            return story;
        }

        /// <summary>
        /// Save story
        /// </summary>
        /// <param name="story">Story entity</param>
        /// <returns></returns>
        public Story Save(Story story)
        {
            Validate(story);
            if (Get(story.StoryID) != null)
                throw new Exception("Existing story");
            story.StoryID = Guid.NewGuid();
            storyList.Add(story);

            return Get(story.StoryID);
        }

        /// <summary>
        /// Access to stories
        /// </summary>
        /// <param name="storyId">Story ID</param>
        /// <returns></returns>
        public Story Get(Guid storyId)
        {
            return storyList.SingleOrDefault(u => u.StoryID == storyId);
        }

        /// <summary>
        /// Access to stories
        /// </summary>
        /// <param name="storyId">Story ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return FakeStoryData.CreateTestStorys();
        }

        /// <summary>
        /// Access to stories
        /// </summary>
        /// <param name="storyId">Story ID</param>
        /// <returns></returns>
        public IList GetByIteration(string iterationId)
        {
            return storyList.FindAll(f => f.IterationID == iterationId);
        }

        /// <summary>
        /// Access to stories
        /// </summary>
        /// <param name="storyId">Story ID</param>
        /// <returns></returns>
        public IList GetByFeature(string releaseId)
        {
            return storyList.FindAll(f => f.Feature != null && f.Feature.FeatureID == new Guid(releaseId));
        }      

        /// <summary>
        /// Delete Story
        /// </summary>
        /// <param name="storyId">Story ID</param>
        /// <returns></returns>
        public void Delete(Guid storyId)
        {
            var story = Get(storyId);
            if (story == null) throw new Exception("NotFound");
            storyList.Remove(story);
        }

        /// <summary>
        /// Update Story
        /// </summary>
        /// <param name="story">Story Entity</param>
        /// <returns></returns>
        public Story Update(Story story)
        {
            Story oldStory = Get(story.StoryID);
            if (oldStory != null)
                storyList.Remove(oldStory);
            else
                story.StoryID = Guid.NewGuid();

            storyList.Add(story);
            return Get(story.StoryID);
        }

        /// <summary>
        /// Access to stories
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return storyList;
        }

        private void Validate(Story story)
        {
            //TODO throw exception on error.
        }        
    }
}
